package user

import (
	"common/mildware"
	"common/models"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"project/pkg/dao/redis"
	"strings"
)

// LikeNickName 昵称模糊
// @Summary 昵称模糊
// @Description 昵称模糊
// @Tags 用户
// @Param nick_name query string true "?nick_name=xxx"
// @Param page query string true "?page=xxx"
// @Param size query string true "?size=xxx"
// @Success 200 {object}  interface{}
// @Router /user/like/nickName [get]
func (h UHandler) LikeNickName(c *gin.Context) {
	resp := new(models.RespData)
	nickName := c.Query("nick_name")
	page, size, err := models.GetPageInfo(c)
	if err != nil {
		resp.SendServerBusy(c)
		return
	}
	//查找
	list, total, err := h.M.UserSearchLikeNickName(nickName, page, size)
	if err != nil {
		zap.L().Error("User:LikeName h.M.UserSearchLikeNickName err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	for _, info := range list {
		//添加状态
		_, err := h.R.GetString(redis.UserStatus + info.UserID)
		if err != nil {
			continue
		}
		info.Status = true
	}
	resp.Data = list
	resp.Count = total
	resp.SendSuccess(c)
}

// LikeUserName 用户名模糊
// @Summary 用户名模糊
// @Description 用户名模糊
// @Tags 用户
// @Param nick_name query string true "?username=xxx"
// @Param page query string true "?page=xxx"
// @Param size query string true "?size=xxx"
// @Success 200 {object}  interface{}
// @Router /user/like/username [get]
func (h UHandler) LikeUserName(c *gin.Context) {
	resp := new(models.RespData)
	username := c.Query("username")
	page, size, err := models.GetPageInfo(c)
	if err != nil {
		resp.SendServerBusy(c)
		return
	}
	//查找
	list, total, err := h.M.UserSearchLikeNickName(username, page, size)
	if err != nil {
		zap.L().Error("User:LikeName h.M.UserSearchLikeNickName err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	for _, info := range list {
		//添加状态
		_, err := h.R.GetString(redis.UserStatus + info.UserID)
		if err != nil {
			continue
		}
		info.Status = true
	}
	resp.Data = list
	resp.Count = total
	resp.SendSuccess(c)
}
func (h UHandler) GetUserInfo(c *gin.Context) {
	resp := new(models.RespData)
	ID, _, _, err := mildware.GetCyrrentUserID(c)
	if err != nil {
		zap.L().Error("User:GetUserInfo mildware.GetCyrrentUserID err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}

	//获得信息
	exist, info, err := h.M.UserCheckId(ID)
	if !exist {
		resp.SendCode(c, models.CodeUserNotExist)
		return
	}
	//添加状态
	_, err = h.R.GetString(redis.UserStatus + info.UserID)
	if err == nil {
		info.Status = true
	}
	resp.Data = info
	resp.SendSuccess(c)

}
func (h UHandler) GetAllFriend(c *gin.Context) {
	var rest []*models.UserInfo
	resp := new(models.RespData)
	ID, _, _, err := mildware.GetCyrrentUserID(c)
	if err != nil {
		zap.L().Error("User:GetUserInfo mildware.GetCyrrentUserID err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	//获得信息
	exist, info, err := h.M.UserCheckId(ID)
	if err != nil {
		zap.L().Error("User:GetUserInfo h.M.UserCheckId err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	if !exist {
		resp.SendCode(c, models.CodeUserNotExist)
		return
	}
	friends := strings.Split(info.Friends, "|")
	for _, friend := range friends {
		if friend == "" {
			continue
		}
		fExist, friendInfo, err := h.M.UserCheckId(friend)
		if err != nil {
			zap.L().Error("User:GetUserInfo h.M.UserCheckId err", zap.Error(err))
			resp.SendServerBusy(c)
			return
		}
		if !exist {
			resp.SendCode(c, models.CodeUserNotExist)
			return
		}
		if fExist {
			//添加状态
			_, err = h.R.GetString(redis.UserStatus + friendInfo.UserID)
			if err == nil {
				friendInfo.Status = true
			}
			rest = append(rest, friendInfo)
		}
	}
	resp.Data = rest
	resp.SendSuccess(c)
}
func (h UHandler) UserInfoByUserID(c *gin.Context) {
	resp := new(models.RespData)
	userId := c.Query("user_id")
	exist, info, err := h.M.UserCheckUserID(userId)
	if err != nil {
		zap.L().Error("User:GetUserInfo h.M.UserCheckUserID err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	if !exist {
		resp.SendCode(c, models.CodeUserNotExist)
		return
	}
	//添加状态
	_, err = h.R.GetString(redis.UserStatus + info.UserID)
	if err == nil {
		info.Status = true
	}
	resp.Data = info
	resp.SendSuccess(c)
}
func (h UHandler) UserInfoByID(c *gin.Context) {
	resp := new(models.RespData)
	id := c.Query("id")
	exist, info, err := h.M.UserCheckId(id)
	if err != nil {
		zap.L().Error("User:GetUserInfo h.M.UserCheckUserID err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	if !exist {
		resp.SendCode(c, models.CodeUserNotExist)
		return
	}
	//添加状态
	_, err = h.R.GetString(redis.UserStatus + info.UserID)
	if err == nil {
		info.Status = true
	}
	resp.Data = info
	resp.SendSuccess(c)
}
